package com.dongdongshop.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.PageBean;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.GoodsVO;
import com.dongdongshop.service.GoodsService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Reference
    private GoodsService goodsService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("listGoods")
    @ResponseBody
    public Result listGoods(String auditStatus, String goodsName, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize){
        try {
            String id = "";
            PageBean<GoodsVO> list = goodsService.listGoods(id, auditStatus, goodsName,pageNum,pageSize);
            return Result.build(ConstantEnum.SUCCESS).setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateStatus")
    @ResponseBody
    public Result updateStatus(@RequestParam("ids[]") Long[] ids,String status){
        goodsService.updateStatusById(ids,status);
        if ("1".equals(status)){
            rocketMQTemplate.convertAndSend("springboot-topic",ids);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
